<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="container">
  @for (topicFilter of httpFiltersFromArray.controls; track topicFilter; let i = $index) {
    <div class="flex flex-1 flex-row items-center justify-start"
      [formGroup]="topicFilter">
      <mat-form-field hideRequiredMarker class="mat-block flex-1" appearance="outline">
        @if (i === 0) {
          <mat-label>{{ i !== 0 ? "" : "integration.topic" | translate }}</mat-label>
        }
        <input matInput required name="topicFilterTopic" formControlName="filter" #inputRef>
        <tb-copy-button matSuffix
                        [class.!hidden]="!topicFilterValue(topicFilter)?.length"
                        [copyText]="topicFilterValue(topicFilter)">
        </tb-copy-button>
        @if (integration() && !subscriptionActive(topicFilter) && inputRef.value?.length && !isEdit() && subscriptionsLoaded) {
          <mat-icon matSuffix style="color: #e33737" [matTooltip]="'integration.inactive-subscription' | translate">
            sync_problem
          </mat-icon>
        }
      </mat-form-field>
      <button mat-icon-button color="primary" [disabled]="httpFiltersFromArray.length === 1 || (!isEdit() && integration())"
        type="button" style="top: 4px; align-self: baseline"
        (click)="httpFiltersFromArray.removeAt(i)"
        [matTooltip]="'integration.remove-topic-filter' | translate" matTooltipPosition="above">
        <mat-icon class="material-icons">close</mat-icon>
      </button>
    </div>
  }
  @if (topicFiltersHasDuplicates()) {
    <mat-error style="font-size: 12px">{{ 'integration.topic-filters-has-duplicates' | translate }}</mat-error>
  }
</div>
@if (!httpFiltersFromArray.disabled) {
  <section style="margin-top: 8px">
    <div class="tb-hint mb-2" innerHTML="{{ 'integration.topic-filters-hint' | translate }}"></div>
    <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.support-level-wildcards' | translate }}"></div>
    <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.single-level-wildcards-hint' | translate }}"></div>
    <div class="tb-hint" innerHTML="{{ 'mqtt-client-session.multi-level-wildcards-hint' | translate }}"></div>
  </section>
}
@if (!httpFiltersFromArray.length) {
  <span class="no-data-found flex items-center justify-center" translate>integration.add-topic-filter-prompt</span>
}
@if (!httpFiltersFromArray.disabled) {
  <button mat-stroked-button color="primary"
    type="button"
    class="add-button"
    (click)="addTopicFilter()"
    [matTooltip]="'integration.add-topic-filter' | translate" matTooltipPosition="above">
    <span translate>integration.add-topic-filter</span>
  </button>
}
